package cn.hotel.mapper;

import cn.hotel.pojo.ClientInformationManagement;
import cn.hotel.pojo.HouseType;
import cn.hotel.pojo.Member;
import cn.hotel.pojo.OrderManagement;
import cn.hotel.utlis.Crud;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class FrontEndMapperImpl extends Crud implements FrontEndMapper {

    //前端注册功能
    @Override
    public int register(ClientInformationManagement clientInformationManagement) {
        String sql = "INSERT INTO CLIENT_INFORMATION_MANAGEMENT(MANAGENAME,SEX,IDENTITYCARD,`PASSWORD`,MOBILEPHONE) VALUES(?,?,?,?,?)";
        List<Object> param = new ArrayList<>();
        param.add(clientInformationManagement.getManageName());
        param.add(clientInformationManagement.getSex());
        param.add(clientInformationManagement.getIdentityCard());
        param.add(clientInformationManagement.getPassword());
        param.add(clientInformationManagement.getMobilePhone());
        int resultSet = this.getInt(sql, param);
        return resultSet;
    }

    //同步到会员表表
    @Override
    public int synchronization(Member member) {
        String sql = "";
        List<Object> param = new ArrayList<>();
        param.add(member);
        int synchronization = this.getInt(sql, param);
        return synchronization;
    }

    //前端展示房型
    @Override
    public List<HouseType> show() {
        String sql = "SELECT HOUSETYPE,PRICE,PARTICULARS,HOUSENUM FROM HOUSE_TYPE ORDER BY ID DESC";
        List<Object> param = new ArrayList<>();
        ResultSet resultSet = this.getResultSet(sql, param);
        List<HouseType> houseTypeList = null;
        try {
            HouseType houseType = null;
            houseTypeList = new ArrayList<>();
            for (int i = 0; resultSet.next(); i++) {
                houseType = new HouseType();
                houseType.setHouseType(resultSet.getString(1));
                houseType.setPrice(resultSet.getString(2));
                houseType.setParticulars(resultSet.getString(3));
                houseType.setHouseNum(resultSet.getInt(4));
                houseTypeList.add(houseType);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            setClear(resultSet);
        }
        return houseTypeList;
    }

    //前端登录显示名字
    @Override
    public ClientInformationManagement checkName(String mobilePhone) {
        String sql = "SELECT MOBILEPHONE,`PASSWORD`,MANAGENAME FROM CLIENT_INFORMATION_MANAGEMENT WHERE MOBILEPHONE = ?";
        List<Object> param = new ArrayList<>();
        param.add(mobilePhone);
        ResultSet resultSet = this.getResultSet(sql, param);
        ClientInformationManagement clientInformationManagement = null;
        try {
            while (resultSet.next()) {
                clientInformationManagement = new ClientInformationManagement();
                clientInformationManagement.setMobilePhone(resultSet.getString(1));
                clientInformationManagement.setPassword(resultSet.getString(2));
                clientInformationManagement.setManageName(resultSet.getString(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            setClear(resultSet);
        }
        return clientInformationManagement;
    }

    //前端根据房型编号显示房型
    @Override
    public HouseType display(Long id) {
        String sql = "SELECT ID,HOUSETYPE,PRICE,PARTICULARS FROM HOUSE_TYPE WHERE ID = ?";
        List<Object> param = new ArrayList<>();
        param.add(id);
        ResultSet resultSet = this.getResultSet(sql,param);
        HouseType houseType = null;
        try {
            while (resultSet.next()){
                houseType = new HouseType();
                houseType.setId(resultSet.getLong(1));
                houseType.setHouseType(resultSet.getString(2));
                houseType.setPrice(resultSet.getString(3));
                houseType.setParticulars(resultSet.getString(4));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            setClear(resultSet);
        }
        return houseType;
    }

    //房型空房数
    @Override
    public int count(int houseTypeId) {
        String sql = "SELECT COUNT(`CONDITION`) FROM ROOM_KEEPER WHERE 1=1 AND `CONDITION` = 0 AND HOUSETYPEID = ? OR `CONDITION` = -1";
        List<Object> param = new ArrayList<>();
        param.add(houseTypeId);
        ResultSet resultSet = this.getResultSet(sql,param);
        int count = 0;
        try {
            while (resultSet.next()){
                count = resultSet.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            setClear(resultSet);
        }
        return count;
    }

    //预定
    @Override
    public int reserve(OrderManagement orderManagement) {
        String slq = "INSERT INTO ORDER_MANAGEMENT(`PASSWORD`,MANAGENAME,CHECKINDATE,CHECKOUTDATE,MOBILEPHONE,IDENTITYCARD" +
                ",PERSONNUM,HOUSETYPEID,PRICE,ROOMNUMBER,REMARK) VALUES(?,?,?,?,?,?,?,?,?,?,?) ";
        List<Object> param = new ArrayList<>();
        param.add(orderManagement.getPassword());
        param.add(orderManagement.getManageName());
        param.add(orderManagement.getCheckInDate());
        param.add(orderManagement.getCheckOutDate());
        param.add(orderManagement.getMobilePhone());
        param.add(orderManagement.getIdentityCard());
        param.add(orderManagement.getPersonNum());
        param.add(orderManagement.getHouseTypeId());
        param.add(orderManagement.getPrice());
        param.add(orderManagement.getRoomNumber());
        param.add(orderManagement.getRemark());
        int result = this.getInt(slq,param);
        return result;
    }

}
